package cn.lanqiao.book.dao.impl;

import cn.lanqiao.book.dao.IBaseDao;
import cn.lanqiao.book.dao.IChapterDao;
import cn.lanqiao.book.pojo.chapter;

import java.util.List;

public class ChapterDaoImplI extends IBaseDao implements IChapterDao {
    @Override
    public void addChapter(chapter chapter) {
        String sql = "insert into b_chapter(b_id,ch_name,ch_content,ch_time) values(?,?,?,now())";
        update(sql, chapter.getB_id(), chapter.getCh_name(),chapter.getCh_content());
    }

    @Override
    public void deleteChapter(String name) {
        String sql = "delete from b_chapter where ch_name = ?";
        update(sql,name);
    }

    @Override
    public List<chapter> queryAll() {
        String sql = "select * from b_chapter";
        return queryForList(chapter.class, sql);
    }

    @Override
    public chapter queryForOne(String name) {
        String sql = "select * from b_chapter where ch_name = ?";
        return queryForOne(chapter.class, sql,name);
    }

    @Override
    public void updateChapter(chapter chapter) {
        String sql = "update b_chapter set b_id =? , ch_name = ? , ch_content = ?,ch_time = now() where ch_Id = ?";
        update(sql, chapter.getB_id(), chapter.getCh_name(), chapter.getCh_content(), chapter.getCh_id());
    }

    @Override
    public List<chapter> search(String name) {
        String sql = "select * from b_chapter where ch_name like ?";
        return queryForList(chapter.class, sql,name);
    }

    @Override
    public List<chapter> content(int id) {
        String sql = "select * from b_chapter where b_id = ?";
        return queryForList(chapter.class, sql,id);
    }

    @Override
    public void deleteChaptervyid(int id) {
        String sql = "delete from b_chapter where b_id = ?";
        update(sql,id);
    }
    @Override
    public void deleteChapterbycid(int id) {
        String sql = "delete from b_chapter where ch_id = ?";
        update(sql,id);
    }


    @Override
    public chapter queryForOnebyid(int id) {
        String sql = "select * from b_chapter where b_id = ? ORDER BY ch_time desc LIMIT 1";
        return queryForOne(chapter.class, sql,id);
    }
    @Override
    public chapter queryForOnebychid(int id) {
        String sql = "select * from b_chapter where ch_id = ?";
        return queryForOne(chapter.class, sql,id);
    }

    @Override
    public chapter queryForOnebyname(int bid, String chname) {
        String sql = "select * from b_chapter where b_id = ? and ch_name = ?";
        return queryForOne(chapter.class, sql,bid,chname);
    }

    @Override
    public chapter shang(int chid,int bid) {
        String sql = "select * from b_chapter where ch_id < ? and b_id =? ORDER BY ch_Id desc LIMIT 1";
        return queryForOne(chapter.class, sql,chid,bid);
    }
    @Override
    public chapter xia(int chid,int bid) {
        String sql = "select * from b_chapter where ch_id > ? and b_id =? ORDER BY ch_Id asc LIMIT 1";
        return queryForOne(chapter.class, sql,chid,bid);
    }

    @Override
    public chapter querybybidchid(int bid, int chid) {
        String sql = "select * from b_chapter where b_id=? and ch_id=?";
        return queryForOne(chapter.class, sql,bid,chid);
    }
}
